package youleche.com.business;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import youleche.com.common.IDFactory;
import youleche.com.connection.ConnectionFactory;
import youleche.com.factory.DaoFactory;
import youleche.com.idao.ILogInfo;
import youleche.com.model.MLogInfo;
import youleche.com.util.DateUtil;

/**
 * 业务类
 * @author Administrator
 *
 */
public class BLogInfo {
	/**
	 * 处理器
	 */
	private ILogInfo handler = null;
	
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 构造器
	 */
	public BLogInfo(){
		this.handler = DaoFactory.getDLogInfo();
	}
	
	/**
	 * 记录异常日志
	 * @param desc 异常描述
	 * @param title 异常标题
	 * @param exception 异常
	 * @param ip ip地址
	 * @throws Exception 抛出异常
	 */
	public int exceptionLog(String desc, String title,
			String exception, String ip) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			MLogInfo logInfo = new MLogInfo();
			logInfo.setLogId(IDFactory.getId("meileche", "logInfo"));
			logInfo.setLogDesc(desc);
			logInfo.setLogTitle(title);
			logInfo.setException(exception);
			logInfo.setIp(ip);
			logInfo.setLogType("0");
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			logInfo.setA1(sdf.format(cal.getTime()));
			logInfo.setUpdateTime(DateUtil.getNowDate());
			return this.handler.addLog(con, logInfo);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}

	/**
	 * 记录acc操作日志
	 * @param desc 异常描述
	 * @param title 异常标题
	 * @param exception 异常
	 * @param ip ip地址
	 * @throws Exception 抛出异常
	 */
	public int accLog(String desc, String title,
			String exception, String ip) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			MLogInfo logInfo = new MLogInfo();
			logInfo.setLogId(IDFactory.getId("meileche", "logInfo"));
			logInfo.setLogDesc(desc);
			logInfo.setLogTitle(title);
			logInfo.setException(exception);
			logInfo.setIp(ip);
			logInfo.setLogType("1");
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			logInfo.setA1(sdf.format(cal.getTime()));
			logInfo.setUpdateTime(DateUtil.getNowDate());
			return this.handler.addLog(con, logInfo);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 得到日志
	 * @param logId 日子ID
	 * @return 返回日志
	 * @throws Exception
	 */
	public MLogInfo getLogInfoById(String logId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			
			return this.handler.getLogInfoById(con, logId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}

	/**
	 * 得到日志信息列表
	 * @param pageIndex 页面索引
	 * @param pageSize 页大小
	 * @return 列表信息
	 * @throws Exception 异常
	 */
	public List<MLogInfo> getLogInfoList(String pageIndex,
			String pageSize) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			
			return this.handler.getLogInfoList(this.con, pageIndex, pageSize);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 得到日志信息总条数
	 * @return 总条数
	 * @throws Exception
	 */
	public String getLogCount() throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			
			return this.handler.getLogCount(this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
}
